package city.spring.controller.estimate;

import city.spring.domain.entity.estimate.EstimateCheckEntity;
import city.spring.modules.oldman.entity.OldmanInfoEntity;
import city.spring.modules.oldman.service.OldmanInfoService;
import city.spring.service.estimate.EstimateCheckService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/estimate-check")
public class EstimateCheckController extends ApiController {

    /**
     * <strong>评估登记</strong> Service
     */
    private final EstimateCheckService estimateCheckService;

    private final OldmanInfoService oldmanInfoService;
    /**
     * 实体类（评估登记）可排序字段
     */
    private final List<SFunction<EstimateCheckEntity, ?>> entityOrderFields;

    public EstimateCheckController(EstimateCheckService estimateCheckService, OldmanInfoService oldmanInfoService) {
        this.estimateCheckService = estimateCheckService;
        this.oldmanInfoService = oldmanInfoService;
        this.entityOrderFields = new ArrayList<>();
    }

    /**
     * 获取全部的 <strong>评估登记</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, EstimateCheckEntity entity) {
        LambdaQueryChainWrapper<EstimateCheckEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>评估登记</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, EstimateCheckEntity entity) {
        LambdaQueryChainWrapper<EstimateCheckEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<EstimateCheckEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        for(EstimateCheckEntity record : page.getRecords()){
            OldmanInfoEntity infoEntity = oldmanInfoService.estimateCheckInfo(record.getElderId());
            infoEntity.setId(record.getElderId());
            record.setElderName(infoEntity.getOldmanName());
            record.setIdCardNumber(infoEntity.getCertificate());
            record.setSex(infoEntity.getSex());
            record.setBirthday(infoEntity.getCertificate().substring(6, 10) + "年" + infoEntity.getCertificate().substring(10, 12) + "月" + infoEntity.getCertificate().substring(12, 14) + "日");
            record.setMedicareCard(infoEntity.getMedicareCard());
            record.setNation(infoEntity.getNation());
            record.setEducationLevel(infoEntity.getEducationLevel());
            record.setReligiousBelief(infoEntity.getReligiousBelief());
            record.setMaritalStatue(infoEntity.getMaritalStatus());
            record.setLivingSituation(infoEntity.getOriginalLivingSituation());
            record.setMedicalInsurance(infoEntity.getMedicalInsurance());
            record.setSourceEconomy(infoEntity.getSourceEconomy());
        }
        return success(page);
    }

    /**
     * 获取一个 <strong>评估登记</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        EstimateCheckEntity entity = estimateCheckService.getById(id);
        OldmanInfoEntity infoEntity = oldmanInfoService.estimateCheckInfo(entity.getElderId());
        infoEntity.setId(entity.getElderId());
        entity.setElderName(infoEntity.getOldmanName());
        entity.setIdCardNumber(infoEntity.getCertificate());
        entity.setSex(infoEntity.getSex());
        entity.setBirthday(infoEntity.getCertificate().substring(6, 10) + "年" + infoEntity.getCertificate().substring(10, 12) + "月" + infoEntity.getCertificate().substring(12, 14) + "日");
        entity.setMedicareCard(infoEntity.getMedicareCard());
        entity.setNation(infoEntity.getNation());
        entity.setEducationLevel(infoEntity.getEducationLevel());
        entity.setReligiousBelief(infoEntity.getReligiousBelief());
        entity.setMaritalStatue(infoEntity.getMaritalStatus());
        entity.setLivingSituation(infoEntity.getOriginalLivingSituation());
        entity.setMedicalInsurance(infoEntity.getMedicalInsurance());
        entity.setSourceEconomy(infoEntity.getSourceEconomy());
        return success(entity);
    }

    /**
     * 添加一个 <strong>评估登记</strong>
     *
     * @param entity 新增的信息
     */
    @PostMapping
    public Object add(@RequestBody EstimateCheckEntity entity) {
        estimateCheckService.saveEstimateCheck(entity);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, entity.getId());
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>评估登记</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody EstimateCheckEntity entity) {
        entity.setId(id);
        estimateCheckService.updateEstimateCheck(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>评估登记</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        estimateCheckService.deleteEstimateCheck(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>评估登记</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        estimateCheckService.deleteEstimateCheck(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象： <strong>评估登记</strong>
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<EstimateCheckEntity> buildLambdaQuery(EstimateCheckEntity entity) {
        LambdaQueryChainWrapper<EstimateCheckEntity> lambdaQuery = estimateCheckService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), EstimateCheckEntity::getId, entity.getId());
        return lambdaQuery;
    }

}
